package com.attendance.sz.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class AdvertUpdater {

	private static Context context;
	private static Handler handler;
	private static ProgressDialog progressDialog;
	private static boolean stop = false;
	private static String strSavefilename;
	private static String strDownUrl;

	// 是否隐藏进度条
	private static boolean bProgressDisp = true;// false为隐藏

	public static void downFile(String url, String savefile, Handler tHandler,
			Context tContext, boolean bShowProgress) {
		strDownUrl = url;
		strSavefilename = savefile;
		context = tContext;
		handler = tHandler;
		bProgressDisp = bShowProgress;
		createProgressDialog();
		progressDialog.show();
		new Thread() {
			public void run() {
				HttpClient client = new DefaultHttpClient();
				HttpGet get = new HttpGet(strDownUrl);
				HttpResponse response;
				try {
					response = client.execute(get);
					HttpEntity entity = response.getEntity();
					long length = entity.getContentLength();
					long llocalsize = OperationFileHelper.getFileSize(new File(strSavefilename));
					if (length == llocalsize){
						if (bProgressDisp) {
							stopProgressDialog();
						}
						Message msg = new Message();
						msg.what = 11;
						msg.obj = "11";
						handler.sendMessage(msg);
						return;
					}
					progressDialog.setMax((int) length);
					InputStream is = entity.getContent();
					FileOutputStream fileOutputStream = null;
					if (is != null) {
						// File file = new File("/mnt/sdcard/",filename);
						File file = new File(strSavefilename);
						// ClientZipfilename =
						// Environment.getExternalStorageDirectory() + "/" +
						// filename;
						// ClientZipfilename = "/mnt/sdcard" + "/" + filename;
						fileOutputStream = new FileOutputStream(file);
						byte[] buf = new byte[1024 * 100];
						int ch = -1;
						int count = 0;
						while ((ch = is.read(buf)) != -1 && !stop) {
							fileOutputStream.write(buf, 0, ch);

							count += ch;
							logMa.d("martrin", "download " + count);
							if (length > 0) {
								// int progress = (int) (count * 100/ length) ;
								int progress = (int) (count);
								if (bProgressDisp) {
									setDownloadProgress(progress);
								}
							}
						}
					}
					fileOutputStream.flush();
					if (fileOutputStream != null) {
						fileOutputStream.close();
					}
					if (bProgressDisp) {
						stopProgressDialog();
					}
					Message msg = new Message();
					msg.what = 11;
					msg.obj = "11";
					handler.sendMessage(msg);
				} catch (ClientProtocolException e) {
					toast("下载文件失败……");
					if (bProgressDisp) {
						stopProgressDialog();
					}
				} catch (IOException e) {
					toast("下载文件失败……");
					if (bProgressDisp) {
						stopProgressDialog();
					}
				}
			}
		}.start();
	}

	private static void setDownloadProgress(final int progress) {
		handler.post(new Runnable() {
			@Override
			public void run() {
				progressDialog.setProgress(progress);
				// logMa.d("martrin", "progress " + progress);
				// if (progress >= 100) {
				// progressDialog.dismiss();
				// //install();
				// sendMessage(101, "");

				// }
			}
		});
	}

	private static void toast(final String msg) {
		handler.post(new Runnable() {

			@Override
			public void run() {
				Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
			}
		});
	}

	private static void stopProgressDialog() {
		handler.post(new Runnable() {

			@Override
			public void run() {
				progressDialog.dismiss();
			}
		});
	}

	public static void createProgressDialog() {
		progressDialog = new ProgressDialog(context);
		progressDialog.setIcon(android.R.drawable.ic_dialog_alert);
		progressDialog.setTitle("文件下载");
		progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		progressDialog.setMax(100);

		progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						stop = true;
						progressDialog.dismiss();
					}
				});
	}

	
	

	//

	// ////////////////////////////////////////////////////////////////////////////

}
